﻿//编辑通知公告
EditNormal_Notice = function (id) {
    Ext.define("NoticeTemplateModel", {
        extend: 'Ext.data.Model',
        proxy: {
            actionMethods: 'post',
            type: 'ajax',
            url: '/manager/Normal_TemplateList/List/notice',
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalPorperty'
            }
        },

        fields: ['Id', 'TemplateTitle']
    });

    //模板
    var NoticeTemplateDs = Ext.create('Ext.data.Store', {
        autoLoad: true,
        pageSize: 1000,
        model: 'NoticeTemplateModel'
    });
    var EditNormal_NoticeForm = Ext.create("Ext.form.Panel", {
        width: 360,
        height: 450,
        autoScroll: true, 
        plain: true,
        layout: "anchor",
        defaultType: "textfield",
        labelWidth: 55,
        baseCls: "x-plain",
        defaults: { labelAlign: 'right', anchor: "90%", msgTarget: "side" },
        buttonAlign: "center",
        bodyStyle: "padding:0 0 0 0",
        items: [{
                       name: "Id",
            fieldLabel: "<font color='red'>*</font>公告递增Id",
            allowBlank: false,
            blankText: "公告递增Id不能为空"
        }, {
            name: "Title",
            fieldLabel: "<font color='red'>*</font>标题",
            allowBlank: false,
            blankText: "标题不能为空"
        }, {
            name: "TextContent",
            xtype: 'textareafield',
            fieldLabel: "<font color='red'>*</font>内容",
            allowBlank: false,
            blankText: "内容不能为空"
         }, {
            xtype:"datefield",
            name: "AddDate",
            fieldLabel: "<font color='red'>*</font>添加时间",
            allowBlank: false,
            format: 'Y-m-d H:i:s',
            blankText: "添加时间不能为空"
        }, {
            name: "Enabled",
            fieldLabel: "状态",
            xtype: "combo",
            //传入后台真实值value field /value
            editable: false,
            anchor: '90%',
            value: 0,
            store: Ext.create('Ext.data.Store', {
                fields: ['show', 'value'],
                data: [
			        { "show": "是", "value": 1 },
			        { "show": "否", "value":0 }
			    ]
            }),
            queryMode: 'local',
            displayField: 'show',
            valueField: 'value'
        }, {
            xtype:"datefield",
            name: "StartDate",
            format: 'Y-m-d H:i:s',
            fieldLabel: "开始时间"
        }, {
            xtype:"datefield",
            name: "EndDate",
            format: 'Y-m-d H:i:s',
            fieldLabel: "结束时间"
        }, {
            name: "UserId",
            fieldLabel: "添加用户",
            regex: /^[\d]{1,10}$/,
            regexText: "请填写1～10个数字"
        }, {
            name: "TemplateId",
            fieldLabel: "<font color='red'>*</font>模板",
            allowBlank: false,
            xtype: "combo",
            //传入后台真实值value field /value
            anchor: '90%',
            store: NoticeTemplateDs,
            value: 0,
            displayField: 'TemplateTitle',
            queryMode: 'local',
            valueField: 'Id',
            listeners: {
                afterRender: function (combo) {
                    combo.setValue(0);
                }
            }
        }
			]
    });

    var EditNormal_NoticeWin = new Ext.Window({
        title: "修改通知公告",
        width: 400,
        height: 450,
        plain: true,
        layout: "fit",
        //iconCls: "addicon",
        //不可以随意改变大小
        resizable: false,
        //是否可以拖动
        //draggable:false,
        defaultType: "textfield",
        //labelWidth: 100,
        collapsible: true, //允许缩放条
        closeAction: 'hide',
        closable: true,
        //弹出模态窗体
        modal: 'true',
        buttonAlign: "center",
        bodyStyle: "padding:10px 0 0 15px",
        items: [EditNormal_NoticeForm],
        listeners: {
            "show": function() {
                if (id && id != "") {
                    //加载表单数据
                    EditNormal_NoticeForm.form.load(
                                {
                                    url: "/manager/Normal_Notice/Look/" + id,
                                    method: "get",
                                    success: function(form, action) {

                                    },
                                    failure: function(form, action) {
                                        Ext.Msg.alert("提示信息", "对不起加载数据失败!请联系管理员");
                                    }
                                }
                    );
                }
            }
        },
        buttons: [{
            text: "保存信息",
            minWidth: 70,
            handler: function() {
                //alert(EditNormal_NoticeForm.getForm().isValid())
                if (EditNormal_NoticeForm.getForm().isValid()) {
                    //弹出效果 Ext.MessageBox.alert("asdf");
                    Ext.MessageBox.show
                                (
                                    {
                                        msg: '正在保存，请稍等...',
                                        progressText: 'Saving...',
                                        width: 300,
                                        wait: true,
                                        waitConfig: { interval: 200 },
                                        icon: 'download',
                                        animEl: 'saving'
                                    }
                                );
                    setTimeout(function() { }, 1000);
                    EditNormal_NoticeForm.form.submit({
                        url: "/manager/Normal_Notice/Update/" + id,
                        method: "POST",
                        success: function(form, action) {
                            //成功后
                            Ext.MessageBox.hide();
                            var flag = action.result.success;
                            if (flag == "true") {
                                Normal_NoticeStore.loadPage(1);
                               Ext.helper.msg("恭喜", "修改成功!");
                                EditNormal_NoticeWin.hide();
                            }
                            else {
                                var message = action.result.message;
                                if (message) {
                                    Ext.MessageBox.alert("修改失败!", message);
                                }
                                else {
                                    Ext.MessageBox.alert("提示!", "修改失败!");
                                    EditNormal_NoticeWin.hide();
                                }

                            }
                        },
                        failure: function(form, action) {
                            Ext.MessageBox.alert("提示!", "修改失败!");
                            EditNormal_NoticeWin.hide();
                        }
                    });
                }
            }
        }, {
            text: "重置",
            minWidth: 70,
            qtip: "重置数据",
            handler: function() {
                //加载表单数据
                EditNormal_NoticeForm.getForm().reset();
                EditNormal_NoticeForm.form.load(
                                {
                                    url: "/manager/Normal_Notice/Look/" + id,
                                    method: "get",
                                    success: function(form, action) {
                                    },
                                    failure: function(form, action) {
                                        //Ext.Msg.alert("提示信息","对不起加载数据失败!请联系管理员");
                                    }
                                }
                    );
            }
        }, {
            text: "取 消",
            minWidth: 70,
            handler: function() {
                EditNormal_NoticeWin.hide();
            }

}]

        });

        EditNormal_NoticeWin.show();
    }
